<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Conversation;
use app\index\model\Users;
class Nearby extends Common
{
  
    /*偶遇接口*/
    public function meetList()
    {   
        $uid = Request::instance()->post('uid','');//用户id
        $lat = Request::instance()->post('lat','');//纬度
        $lng = Request::instance()->post('lng','');//经度
        $gender = Request::instance()->post('gender','3');//性别 0女 1男 2全部
        $age_start = Request::instance()->post('age_start','');//年龄开始时间 
        $age_end= Request::instance()->post('age_end','');//年龄结束时间 
        $province = Request::instance()->post('province','');//所在省份 
        $type_id = Request::instance()->post('eduction','');//学历类型 0不限 1专科 2本科 3硕士 4博士 
        file_put_contents('filenhao.txt',$type_id);
        $marriage = Request::instance()->post('marriage');//婚姻状况 1单身 2离异 0不限
        $page=input('page');//页面值 
        if(empty($page)){
          return json(['state'=>'203','msg'=>'页面值为空']);
        }
        $limit =5;//限制条数
        $start = ($page-1)*$limit;
        $where="userFlag=1 and member_type=1 and palState=1";//判断用户是否被删除了与是否关闭啦交友状态
        //性别
        if($gender!=3 && $gender<2){
         $where=$where." and gender=$gender";
        }
        //年龄
        if(!empty($age_start) && !empty($age_end)){
           $where=$where." and age between  $age_start and $age_end";
        }
        //所在省份
        if(!empty($province)){
           $where=$where." and province='$province'";
        }
        //学历类型
        if(!empty($type_id)){
           $where=$where." and type_id=$type_id";
        }
        //婚姻状况
        if(!empty($marriage)){
           $where=$where." and marriage=$marriage";
        }
         $result=Conversation::sel($where,$start,$limit);
        if(!empty($uid)){
          $where=$where." and uid!=$uid";
          $result=Conversation::sel($where,$start,$limit);
          if(empty($result)){
            return json(['state'=>'201','msg'=>'查询内容不存在']);
           }
           if(empty($lat) || empty($lng)){
             return json(['state'=>'203','msg'=>'用户的位置为空']);
           }
           $sdata1=['latitude'=>$lat,'longitude'=>$lng];
           Db::name('user_info')->where(['uid'=>$uid,'member_type'=>'1'])->update($sdata1);
            foreach ($result as  $key=>$val) {
          $result[$key]['list'] = $this->getDistance($lng, $lat, $val['longitude'], $val['latitude'], 2);
          if($result[$key]['list']>999){
              $result[$key]['list']=999;
             }
          }
         // //循环遍历
         // foreach($result as $k => $v){
         //    $sort['list'][$k] = $v['list'];
         // }
         // //先按距离升序排序
         // array_multisort($sort['list'],SORT_ASC,$result);
        }
       $result=array_values($result);
       if($result){
        return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
       }else{
        return json(['state'=>'201','msg'=>'查询内容不存在']);
       } 
    }
    //好友列表接口
    public function friendList(){
        $uid = Request::instance()->post('uid','');//用户id
        $stateId=input('stateId');//切换状态 1关注 2粉丝
       // $page=input('page');//获取页码值 
        if(empty($uid) || empty($stateId)){
          return json(['state'=>'202','msg'=>'参数不足']);
        }
        // if(empty($page)){
        //   return json(['start'=>'204','msg'=>'页码值为空']);
        // }
        // $limit =3;//限制条数
        // $start = ($page-1)*$limit;
        $rs=Users::selOne($uid);
        $nowTime=time();//获取现在的距离
        if($rs){
          $user_relation=Db::name('user_relation')->field('id as fId,uid,follow_uid,fans_uid,status as 
             focus')->where(['uid'=>$uid])->select();
          if($stateId==1){
            foreach($user_relation as $key=>$val){
             if($val['focus']==0){
               $user_relation[$key]['focus']='2';
             }
               $user_info=Db::name('user_info')
               ->where(['uid'=>$val['follow_uid'],'userFlag'=>1])
               ->field('uid as nearby_id,nickname,city,userPhoto,gender,birthday,constellation,university,token_time,char_length(sef_desc) as sef_desc')
               ->find();
               if(!empty($user_info)){
                    if($nowTime>$user_info['token_time']){
                         $user_info['state']='-1';
                    }else{
                         $user_info['state']='1';
                    }
                 $user_relation[$key]['user']=$user_info;
                 }else{
                   unset($user_relation[$key]);
                 }
               
           }
          }else{
            foreach($user_relation as $key=>$val){
              if($val['focus']==0){
               $user_relation[$key]['focus']='3';
             }
               $user_info=Db::name('user_info')->where(['uid'=>$val['fans_uid'],'userFlag'=>1])->field('uid as nearby_id,nickname,userPhoto,city,gender,birthday,constellation,university,token_time')->find();
               if(!empty($user_info)){
                    if($nowTime>$user_info['token_time']){
                         $user_info['state']='-1';
                    }else{
                         $user_info['state']='1';
                    }
                $user_relation[$key]['user']=$user_info;
                 }else{
                   unset($user_relation[$key]);
                 }
          }
        }
       $user_relation=array_values($user_relation);
         if($user_relation){
          return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$user_relation]));
         }else{
          return json(['state'=>'201','msg'=>'查询内容不存在']);
         }
        }else{
           return json(['state'=>'203','msg'=>'用户已不存在']);
        }
    }
    //玩过的活动接口
    public function activityPlay(){
       $uid = Request::instance()->post('uid','');//用户id
        if(empty($uid)){
          return json(['state'=>'202','msg'=>'用户为空']);
        }
       $result=Conversation::actPlay($uid);
       if($result){
        return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
       }else{
        return json(['state'=>'201','msg'=>'查询内容不存在']);
       }
    }
    //交友状态
    public function palState(){
      $uid=input('uid');
      if(empty($uid)){
        return  json(['state'=>'202','msg'=>'用户为空']);
      }
      $result=Db::name('user_info')->where(['uid'=>$uid,'userFlag'=>'1'])->field('palState')->find();
       if($result){
        return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
       }else{
        return json(['state'=>'201','msg'=>'用户已不存在']);
       }
    }
    //设置交友状态
    public function setPal(){
        $uid=input('uid');
        $palState=input('palState');
        if(empty($uid) || empty($palState)){
          return  json(['state'=>'202','msg'=>'请求参数不足']);
        }
        $result=Db::name('user_info')->where(['uid'=>$uid,'userFlag'=>'1'])->setField('palState',$palState);
         if($result){
          return json(['state'=>'200','msg'=>'设置成功','result'=>$result]);
         }else{
          return json(['state'=>'201','msg'=>'网络错误再试试']);
         }
    }
        
}


